package com.xumingxun.aibox.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xumingxun.aibox.context.Context;
import com.xumingxun.aibox.dao.ApplicationRepository;
import com.xumingxun.aibox.domain.dto.Result;
import com.xumingxun.aibox.domain.entity.Application;
import com.xumingxun.aibox.domain.node.StartNode;
import org.apache.commons.io.IOUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class ApiController {

    @Resource
    ApplicationRepository applicationRepository;

    @GetMapping("/valid/application/5")
    public Object valid(){
        return Result.success(true);
    }

    @GetMapping("/profile")
    public Object profile(){
        return JSONObject.parseObject("{\n" +
                "    \"code\": 200,\n" +
                "    \"message\": \"\\u6210\\u529f\",\n" +
                "    \"data\": {\n" +
                "        \"version\": \"v1.10.2-lts (build at 2025-03-05T19:39, commit: bc3dcda1)\",\n" +
                "        \"IS_XPACK\": false,\n" +
                "        \"XPACK_LICENSE_IS_VALID\": false\n" +
                "    }\n" +
                "}");
    }
    @GetMapping("/user")
    public Object user(){
        return JSONObject.parseObject("{\n" +
                "    \"code\": 200,\n" +
                "    \"message\": \"\\u6210\\u529f\",\n" +
                "    \"data\": {\n" +
                "        \"id\": \"f0dd8f71-e4ee-11ee-8c84-a8a1595801ab\",\n" +
                "        \"username\": \"admin\",\n" +
                "        \"email\": \"\",\n" +
                "        \"role\": \"ADMIN\",\n" +
                "        \"permissions\": [\n" +
                "            \"DATASET:MANAGE:34719184-0c5a-11f0-a556-0242ac11000e\",\n" +
                "            \"DATASET:USE:34719184-0c5a-11f0-a556-0242ac11000e\",\n" +
                "            \"DATASET:DELETE:34719184-0c5a-11f0-a556-0242ac11000e\",\n" +
                "            \"APPLICATION:MANAGE:1027f6ba-0caa-11f0-9ebe-0242ac11000e\",\n" +
                "            \"APPLICATION:USE:1027f6ba-0caa-11f0-9ebe-0242ac11000e\",\n" +
                "            \"APPLICATION:DELETE:1027f6ba-0caa-11f0-9ebe-0242ac11000e\",\n" +
                "            \"APPLICATION:MANAGE:576f334e-0c5a-11f0-af1f-0242ac11000e\",\n" +
                "            \"APPLICATION:USE:576f334e-0c5a-11f0-af1f-0242ac11000e\",\n" +
                "            \"APPLICATION:DELETE:576f334e-0c5a-11f0-af1f-0242ac11000e\",\n" +
                "            \"APPLICATION:MANAGE:be8ad1f2-0f0a-11f0-9695-0242ac110003\",\n" +
                "            \"APPLICATION:USE:be8ad1f2-0f0a-11f0-9695-0242ac110003\",\n" +
                "            \"APPLICATION:DELETE:be8ad1f2-0f0a-11f0-9695-0242ac110003\",\n" +
                "            \"APPLICATION:MANAGE:8b86b2c8-219b-11f0-8dbc-0242ac110009\",\n" +
                "            \"APPLICATION:USE:8b86b2c8-219b-11f0-8dbc-0242ac110009\",\n" +
                "            \"APPLICATION:DELETE:8b86b2c8-219b-11f0-8dbc-0242ac110009\",\n" +
                "            \"APPLICATION:MANAGE:1d19a14a-0c62-11f0-a556-0242ac11000e\",\n" +
                "            \"APPLICATION:USE:1d19a14a-0c62-11f0-a556-0242ac11000e\",\n" +
                "            \"APPLICATION:DELETE:1d19a14a-0c62-11f0-a556-0242ac11000e\",\n" +
                "            \"APPLICATION:MANAGE:d8285a84-236f-11f0-918f-0242ac110009\",\n" +
                "            \"APPLICATION:USE:d8285a84-236f-11f0-918f-0242ac110009\",\n" +
                "            \"APPLICATION:DELETE:d8285a84-236f-11f0-918f-0242ac110009\",\n" +
                "            \"APPLICATION:MANAGE:510f8616-14f2-11f0-9737-0242ac110003\",\n" +
                "            \"APPLICATION:USE:510f8616-14f2-11f0-9737-0242ac110003\",\n" +
                "            \"APPLICATION:DELETE:510f8616-14f2-11f0-9737-0242ac110003\",\n" +
                "            \"USER:READ\",\n" +
                "            \"USER:EDIT\",\n" +
                "            \"DATASET:CREATE\",\n" +
                "            \"DATASET:READ\",\n" +
                "            \"DATASET:EDIT\",\n" +
                "            \"APPLICATION:READ\",\n" +
                "            \"APPLICATION:CREATE\",\n" +
                "            \"APPLICATION:DELETE\",\n" +
                "            \"APPLICATION:EDIT\",\n" +
                "            \"SETTING:READ\",\n" +
                "            \"MODEL:READ\",\n" +
                "            \"MODEL:EDIT\",\n" +
                "            \"MODEL:DELETE\",\n" +
                "            \"MODEL:CREATE\",\n" +
                "            \"TEAM:READ\",\n" +
                "            \"TEAM:CREATE\",\n" +
                "            \"TEAM:DELETE\",\n" +
                "            \"TEAM:EDIT\"\n" +
                "        ],\n" +
                "        \"is_edit_password\": false,\n" +
                "        \"language\": null\n" +
                "    }\n" +
                "}");
    }
    @GetMapping("/user/list/APPLICATION")
    public Object userListApplication(){
        return JSONObject.parseObject("{\n" +
                "    \"code\": 200,\n" +
                "    \"message\": \"\\u6210\\u529f\",\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"id\": \"all\",\n" +
                "            \"username\": \"\\u5168\\u90e8\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"id\": \"f0dd8f71-e4ee-11ee-8c84-a8a1595801ab\",\n" +
                "            \"username\": \"\\u6211\\u7684\"\n" +
                "        }\n" +
                "    ]\n" +
                "}");
    }


    @PostMapping("/user/login")
    public Object userLogin(){
        return JSONObject.parseObject("{\n" +
                "    \"code\": 200,\n" +
                "    \"message\": \"\\u6210\\u529f\",\n" +
                "    \"data\": \"eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOiJmMGRkOGY3MS1lNGVlLTExZWUtOGM4NC1hOGExNTk1ODAxYWIiLCJlbWFpbCI6IiIsInR5cGUiOiJVU0VSIn0:1u9I9n:ZAcOOVWMpLv_c6H-jW2ajYrBBWTUj3K7rwmOQw_9frM\"\n" +
                "}");
    }
//    @GetMapping("/application")
//    public Result application() throws IOException {
//        return Result.success(applicationRepository.findAll().stream().map(item->JSONObject.parseObject(item.getJson())).collect(Collectors.toList()));
//    }




    @GetMapping("/provider")
    public Object provider() throws IOException {
        return JSONObject.parseObject(FileUtil.readString(ResourceUtils.getFile("classpath:provider.json"), "utf-8"));
    }

}
